<template>
    <div>
        <el-form :model="outboundOrder" label-width="120px" ref="outboundOrder_refs">
            <el-row>
                <el-col :span="9" :offset="2">
                    <el-form-item label="领用人" prop="EmployeeId">
                        <el-select v-model="outboundOrder.EmployeeId" filterable placeholder="领用人" style="width:100%">
                            <el-option v-for="v in AllUsers" v-if="v.InternshipState!=8" :value="v.Id" :label="v.UserName"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="9" :offset="1">
                    <el-form-item label="领用部门" prop="DepartmentId">
                        <el-select v-model="outboundOrder.DepartmentId" filterable placeholder="请选择部门" style="width:100%">
                            <el-option v-for="v in AllDepartment" :value="v.Id" :label="v.DepartmentName"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="9" :offset="2">
                    <el-form-item label="领用项目" prop="OutboundProject">
                        <el-select v-model="outboundOrder.OutboundProject" filterable placeholder="请选择项目" style="width:100%">
                            <el-option v-for="v in AllProject" :value="v.Id" :label="v.ProjectName"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="9" :offset="1"> 
                    <el-form-item label="运输方式" prop="transport">
                        <el-select v-model="outboundOrder.transport" placeholder="请选择运输方式" style="width:100%">
                            <el-option :value="0" label="无"></el-option>
                            <el-option :value="1" label="货运"></el-option>
                            <el-option :value="2" label="物流"></el-option>
                            <el-option :value="4" label="快递"></el-option>
                            <el-option :value="8" label="人工"></el-option>
                            <el-option :value="16" label="空运"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="19" :offset="2">
                    <el-form-item label="物资列表">
                        <el-table border stripe :data="tableData" ref="multipleTable">
                            <el-table-column prop="DeviceNumber" show-overflow-tooltip align="center" label="物资编号"></el-table-column>
                            <el-table-column label="名称" prop="DeviceName" align="center"></el-table-column>
                            <el-table-column label="规格型号" prop="DeviceModel" align="center"></el-table-column>
                            <el-table-column prop="StockAmount" show-overflow-tooltip align="center" label="库存数量"></el-table-column>
                            <el-table-column label="数量" prop="Amount" align="center">
                                <template slot-scope="slot">
                                    <el-input v-model="slot.row.Amount"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column label="单位" prop="Unit" align="center"></el-table-column>
                            <!--<el-table-column label="单价" prop="Price" align="center">
                                <template slot-scope="slot">
                                    <el-input v-model="slot.row.Price"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column label="总价" prop="TotalPrice" align="center">
                                <template slot-scope="slot">
                                    {{ slot.row.Amount * slot.row.Price }}
                                </template>
                            </el-table-column>-->
                            <el-table-column label="操作" align="center">
                                <template slot-scope="slot">
                                    <el-button icon="el-icon-delete" size="mini" type="danger" @click="TableRemove_event(slot.row)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
              <el-col :span="19" :offset="2">
                  <el-form-item prop="Purpose" label="用途">
                      <el-input type="texarea" v-model="outboundOrder.Purpose"></el-input>
                  </el-form-item>
              </el-col>
            </el-row>
            <el-row>
                <el-col :span="19" :offset="2">
                    <el-form-item label="备注" prop="Remark">
                        <el-input type="texarea" v-model="outboundOrder.Remark"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col style="text-align: center">
                    <el-button type="warning" @click="AddExistingMaterial_dialog=true">导入物资</el-button>
                    <el-button type="primary" @click="SaveDraft_event">保存草稿</el-button>
                    <el-button type="success" @click="Submit_event">提交审核</el-button>
                </el-col>
            </el-row>
        </el-form>

        <el-dialog :visible.sync="AddExistingMaterial_dialog" :modal="false" width="90%" top="10px" title="现有物资导入">
            <AddExistingMaterial :selection_-material="tableData" :all-device-state="AllDeviceState" :all-device-category="AllDeviceCategory" :all-department="AllDepartment"></AddExistingMaterial>
        </el-dialog>
    </div>
</template>

<script>
    import AddExistingMaterial from "./AddExistingMaterial";
    import * as publicjs from "/public/Scriput/Public"

    export default {
        name: "NewMaterialDelivery",
        components: {
            "AddExistingMaterial": AddExistingMaterial,
        },
        props: {
            AllDeviceCategory: {
                type: Array,
                required: true
            },
            AllDepartment: {
                type: Array,
                required: true
            },
            AllDeviceState: {
                type: Array,
                required: true
            }
        },
        data() {
            return {
                outboundOrder: {
                    EmployeeId: '', //领用人
                    DepartmentId: '', //领用部门
                    OutboundProject: '', //领用项目
                    transport: '', //运输方式
                    Remark: '',
                    Purpose: '', //用途
                },
                tableData: [],
                //现有资源弹框显示
                AddExistingMaterial_dialog: false,
                // 所有的项目，排除已删除项目
                AllProject: [],
                // 所有未删除的人员,包括离职人员
                AllUsers: [],

            }
        },
        created() {
            this.AllProject = publicjs.GetAllProject(this.$http)
            this.AllUsers = publicjs.GetCompanyAllUsers(this.$http)
        },
        methods: {
            //表格数据移除
            TableRemove_event(obj) {
                this.tableData.forEach((item, i) => {
                    if (item.Id == obj.Id) {
                        this.tableData.splice(i, 1)
                    }
                })
            },
            // 审核提交事件
            async Submit_event() {
                //let result = await publicjs.Isjurisdiction(this.$http,'AddOutboundRight')
                //if(!result){
                //    this.$message.warning('您不具有当前操作的权限，请联系系统管理员')
                //    return
                //}
                if (this.tableData.length <= 0) {
                    this.$message.warning('物资列表不能为空')
                    return
                }
                for (let i = 0; i < this.tableData.length; i++) {
                    if (this.tableData[i].Amount == "0") {
                        this.$message.warning('物品出库数量不能为0')
                        return;
                    }
                    if (this.tableData[i].Amount > this.tableData[i].StockAmount) {
                        this.$message.warning('出库数量不能大于库存数量')
                        return;
                    }
                    if (this.tableData[i].Id == 0)
                        this.tableData[i].Id = this.tableData[i].DeviceId;
                }
                let loadding = this.openLoading();
                if (this.outboundOrder.OutboundProject == "")
                    this.outboundOrder.OutboundProject = 0;
                if (this.outboundOrder.transport == "")
                    this.outboundOrder.transport = 0;
                this.$http.post('Device/AddOutboundOrderContent', {
                    outboundOrder: this.outboundOrder,
                    devices: this.tableData,
                }).then(res => {
                    loadding.close();
                    if (res.data.IsSuccess) {
                        this.$message.success('添加出库单成功')
                        this.dialog_close()
                    } else {
                        this.$message.warning(res.data.Msg)
                    }
                })
            },
            //草稿保存
            SaveDraft_event() {
                if (this.tableData.length <= 0) {
                    this.$message.warning('物资列表不能为空')
                    return
                }
                for (let i = 0; i < this.tableData.length; i++) {
                    if (this.tableData[i].Amount == "0") {
                        this.$message.warning('物品出库数量不能为0')
                        return;
                    }
                    if (this.tableData[i].Amount > this.tableData[i].StockAmount) {
                        this.$message.warning('出库数量不能大于库存数量')
                        return;
                    }
                    if (this.tableData[i].Id == 0)
                        this.tableData[i].Id = this.tableData[i].DeviceId;
                }
                let loadding = this.openLoading();
                if (this.outboundOrder.OutboundProject == "")
                    this.outboundOrder.OutboundProject = 0;
                if (this.outboundOrder.transport == "")
                    this.outboundOrder.transport = 0;
                this.$http.post('Device/SaveDraft', {
                    outboundOrder: this.outboundOrder,
                    devices: this.tableData,
                }).then(res => {
                    loadding.close();
                    if (res.data.IsSuccess) {
                        this.$message.success('保存出库单成功')
                        this.dialog_close()
                    } else {
                        this.$message.warning(res.data.Msg)
                    }
                })
            },
            //窗体关闭表格处理
            dialog_close() {
                // 重置表单数据
                this.$refs['outboundOrder_refs'].resetFields()
                this.$parent.$parent.NewMaterial_delivery_dialog=false
                this.tableData = []
            }
        }
    }
</script>

<style scoped>

</style>
